﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Qplus.Infrastructure;
using Qplus.protocol;
using System.Diagnostics;
using Qplus.beans;
using Qplus.enums;

namespace Qplus.UI
{
    public class RoomListener : IPacketListener
    {
        bool isFromPicTask = false;
        static RoomListener instance;

        public event EventHandler<UIEvent> EnterEvent;
        public event EventHandler<UIEvent> ReceiveMsgEvent;

        public RoomListener()
        {
            
        }

        public static RoomListener Instance
        {
            get
            {
                if (null == instance)
                    instance = new RoomListener();
                return instance;
            }
        }

        public bool IsFromPicTask { get { return isFromPicTask; } set { isFromPicTask = value; } }

        public virtual void packetEvent(PacketEvent Pevent)
        {
            InPacket packet = Pevent.myPacket;
            if (packet == null)
            {
                return;
            }
            else
            {
                int id = packet.getMsgID();
                MMessageID MID = (MMessageID)packet.getMsgID();
                switch ((MMessageID)packet.getMsgID())
                {
                    case MMessageID.U2C_RESP_ROOM_PEOPLE_COUNT:
                    case MMessageID.U2C_RESP_ENTER_ROOM:
                        {
                            U2C_RESP_ENTER_ROOM p = ((U2C_RESP_ENTER_ROOM)packet);
                            Debug.WriteLine(p.getMsgID().ToString());
                            Room.CurrentRoomInstance.RoomID = p.getRoomID();
                            processEnterRoomMsg();
                            break;
                        }
                    case MMessageID.U2C_RESP_LEAVE_ROOM:
                    case MMessageID.U2C_RESP_ROOM_SENDMSG:
                        {
                            //do something
                            break;
                        }
                    case MMessageID.U2C_NOTIFY_ROOM_RECVTMSG:
                        {
                            processReceiveMsgEvent(packet.getData());
                            break;
                        }
                    default:
                        break;
                }
            }
            //InPacket packet = Pevent.myPacket;
            //if (packet == null)
            //{
            //    return;
            //}
            //else if (packet is U2C_RESP_ENTER_ROOM)
            //{
            //    U2C_RESP_ENTER_ROOM p = ((U2C_RESP_ENTER_ROOM)packet);
            //    Debug.WriteLine(p.getMsgID().ToString());
            //    Room.CurrentRoomInstance.RoomID = p.getRoomID();
            //    processEnterRoomMsg();
            //}
            //else if (packet is U2C_RESP_LEAVE_ROOM)
            //{

            //}
            //else if (packet is U2C_RESP_ROOM_SENDMSG)
            //{
            //    //seccess or fail 
            //}
            //else if (packet is U2C_NOTIFY_ROOM_RECVTMSG)
            //{
            //    processReceiveRoomMsg(packet.getData());
            //}

        }

        private void processEnterRoomMsg()
        {
            if (null != EnterEvent)
                EnterEvent(this, new UIEvent());
        }

        private void processReceiveMsgEvent(byte[] msg)
        {
            if (null != ReceiveMsgEvent)
                ReceiveMsgEvent(this, new UIEvent(msg));
        }
    }
}
